package com.sudo.service.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.LocalDateTimeUtil;
import com.sudo.dao.admin.entity.SysUserBehavior;
import com.sudo.dao.admin.entity.SysUserLoginRecord;
import com.sudo.dao.admin.entity.SysUserStsMonth;
import com.sudo.dao.admin.mapper.SysUserBehaviorMapper;
import com.sudo.dao.admin.mapper.SysUserLoginRecordMapper;
import com.sudo.dao.admin.mapper.SysUserStsMonthMapper;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysUserStsMonthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class SysUserStsMonthServiceImpl extends AdminBaseService implements SysUserStsMonthService {

    @Resource
    private SysUserBehaviorMapper sysUserBehaviorMapper;

    @Resource
    private SysUserLoginRecordMapper sysUserLoginRecordMapper;

    @Resource
    private SysUserStsMonthMapper sysUserStsMonthMapper;


    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void stsUserBehaviorByMonth(int year, int month) {
        Example deleteExample = new Example(SysUserStsMonth.class);
        Example.Criteria criteria = deleteExample.createCriteria();
        criteria.andEqualTo("year", year);
        criteria.andEqualTo("month", month);
        sysUserStsMonthMapper.deleteByExample(deleteExample);

        LocalDate monthBegin = LocalDateTimeUtil.parseMonthBegin(year, month);
        LocalDate monthEnd = LocalDateTimeUtil.parseMonthEnd(monthBegin);

        // 某一月的活跃度（操作数）
        SysUserStsMonth sysUserStsMonth = new SysUserStsMonth();
        Example userBehaviorExample = new Example(SysUserBehavior.class);
        Example.Criteria userBehaviorCriteria = userBehaviorExample.createCriteria();
        userBehaviorCriteria.andGreaterThanOrEqualTo("operTime", monthBegin);
        userBehaviorCriteria.andLessThanOrEqualTo("operTime", monthEnd);
        int numBehavior = sysUserBehaviorMapper.selectCountByExample(userBehaviorExample);
        sysUserStsMonth.setNumBehavior(numBehavior);

        // 某一月的登录人次
        Example userLoginNotExample = new Example(SysUserLoginRecord.class);
        Example.Criteria userLoginNotCriteria = userLoginNotExample.createCriteria();
        userLoginNotCriteria.andGreaterThanOrEqualTo("loginTime",monthBegin);
        userLoginNotCriteria.andLessThanOrEqualTo("loginTime", monthEnd);
        int loginNot = sysUserLoginRecordMapper.selectCountByExample(userLoginNotExample);
        sysUserStsMonth.setLoginNot(loginNot);

        // 某一月的登录人数（distinct）
        // selectCountByExample userLoginNopExample.setDistinct(true) 不生效, 只在 selectByExample 生效
        int loginNop = sysUserLoginRecordMapper.selectUserNopCountByDay(monthBegin.toString(),monthEnd.toString());
        sysUserStsMonth.setLoginNop(loginNop);

        LocalDateTime createTime = LocalDateTime.now();
        sysUserStsMonth.setCreateTime(createTime);
        sysUserStsMonth.setYear(year);
        sysUserStsMonth.setMonth(month);
        sysUserStsMonth.setStartDate(monthBegin);
        sysUserStsMonth.setEndDate(monthEnd);

        sysUserStsMonthMapper.insertSelective(sysUserStsMonth);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public SysUserStsMonth getUserStsMonth(int year, int month) {
        Example example = new Example(SysUserStsMonth.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("year", year);
        criteria.andEqualTo("month", month);
        SysUserStsMonth sysUserStsMonth = sysUserStsMonthMapper.selectOneByExample(example);
        if (null == sysUserStsMonth) {
            sysUserStsMonth = new SysUserStsMonth(year, month);
        }
        return sysUserStsMonth;
    }

    @Override
    public List<SysUserStsMonth> getSysUserStsMonthList(LocalDate localDate) {
        LocalDate localDate1 = localDate.minusMonths(12);
        int startYear = localDate1.getYear();
        int endYear = localDate.getYear();

        Example example = new Example(SysUserStsMonth.class);
        example.orderBy("year").desc();
        example.orderBy("month").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThanOrEqualTo("year",startYear);
        criteria.andLessThanOrEqualTo("year",endYear);
        PageHelper.startPage(1, 12);
        List<SysUserStsMonth> sysUserStsMonthList = sysUserStsMonthMapper.selectByExample(example);
        Collections.reverse(sysUserStsMonthList);
        return sysUserStsMonthList;
    }
}
